<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单据编号" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织" prop="fcompany">
        <el-input
          v-model="queryParams.fcompany"
          placeholder="请输入组织"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="薪酬期间" prop="fperiod">-->
<!--        <el-input-->
<!--          v-model="queryParams.fperiod"-->
<!--          placeholder="请输入薪酬期间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="制单人" prop="fcreator">-->
<!--        <el-input-->
<!--          v-model="queryParams.fcreator"-->
<!--          placeholder="请输入制单人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="制单时间" prop="fcreatetime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.fcreatetime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择制单时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后修改人" prop="flastupdateuser">-->
<!--        <el-input-->
<!--          v-model="queryParams.flastupdateuser"-->
<!--          placeholder="请输入最后修改人"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后修改时间" prop="flastupdatetime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.flastupdatetime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择最后修改时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="状态" prop="fstate">-->
<!--        <el-input-->
<!--          v-model="queryParams.fstate"-->
<!--          placeholder="请输入状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="来源单ID" prop="fsourcebillid">-->
<!--        <el-input-->
<!--          v-model="queryParams.fsourcebillid"-->
<!--          placeholder="请输入来源单ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basebill:cwsgtzs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basebill:cwsgtzs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basebill:cwsgtzs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basebill:cwsgtzs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cwsgtzsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="fid" />
      <el-table-column label="单据编号" align="center" prop="fnumber" />
      <el-table-column label="组织" align="center" prop="fcompany" />
      <el-table-column label="薪酬期间" align="center" prop="fperiod" />
      <el-table-column label="制单人" align="center" prop="fcreator" />
      <el-table-column label="制单时间" align="center" prop="fcreatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fcreatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后修改人" align="center" prop="flastupdateuser" />
      <el-table-column label="最后修改时间" align="center" prop="flastupdatetime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flastupdatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="fstate" />
      <el-table-column label="来源单ID" align="center" prop="fsourcebillid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basebill:cwsgtzs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basebill:cwsgtzs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改手工调整数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单据编号" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入单据编号" />
        </el-form-item>
        <el-form-item label="组织" prop="fcompany">
          <el-input v-model="form.fcompany" placeholder="请输入组织" />
        </el-form-item>
        <el-form-item label="薪酬期间" prop="fperiod">
          <el-input v-model="form.fperiod" placeholder="请输入薪酬期间" />
        </el-form-item>
        <el-form-item label="状态" prop="fstate">
          <el-input v-model="form.fstate" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="来源单ID" prop="fsourcebillid">
          <el-input v-model="form.fsourcebillid" placeholder="请输入来源单ID" />
        </el-form-item>
        <el-divider content-position="center">手工调整数据分录信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWfzCwsgtzsentry">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWfzCwsgtzsentry">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wfzCwsgtzsentryList" :row-class-name="rowWfzCwsgtzsentryIndex" @selection-change="handleWfzCwsgtzsentrySelectionChange" ref="wfzCwsgtzsentry">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="成本中心" prop="fcostnumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcostnumber" placeholder="请输入成本中心" />
            </template>
          </el-table-column>
          <el-table-column label="成本名称" prop="fcostname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fcostname" placeholder="请输入成本名称" />
            </template>
          </el-table-column>
          <el-table-column label="会计科目编码" prop="faccountnumber" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.faccountnumber" placeholder="请输入会计科目编码" />
            </template>
          </el-table-column>
          <el-table-column label="会计科目名称" prop="faccountname" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.faccountname" placeholder="请输入会计科目名称" />
            </template>
          </el-table-column>
          <el-table-column label="金额" prop="ftzje" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.ftzje" placeholder="请输入金额" />
            </template>
          </el-table-column>
          <el-table-column label="摘要" prop="fzy" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fzy" placeholder="请输入摘要" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCwsgtzs, getCwsgtzs, delCwsgtzs, addCwsgtzs, updateCwsgtzs } from "@/api/basebill/cwsgtzs";

export default {
  name: "Cwsgtzs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWfzCwsgtzsentry: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 手工调整数据表格数据
      cwsgtzsList: [],
      // 手工调整数据分录表格数据
      wfzCwsgtzsentryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fnumber: null,
        fcompany: null,
        fperiod: null,
        fcreator: null,
        fcreatetime: null,
        flastupdateuser: null,
        flastupdatetime: null,
        fstate: null,
        fsourcebillid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fid: [
          { required: true, message: "ID不能为空", trigger: "blur" }
        ],
        fnumber: [
          { required: true, message: "单据编号不能为空", trigger: "blur" }
        ],
        fcompany: [
          { required: true, message: "组织不能为空", trigger: "blur" }
        ],
        fperiod: [
          { required: true, message: "薪酬期间不能为空", trigger: "blur" }
        ],
        fcreator: [
          { required: true, message: "制单人不能为空", trigger: "blur" }
        ],
        fcreatetime: [
          { required: true, message: "制单时间不能为空", trigger: "blur" }
        ],
        flastupdateuser: [
          { required: true, message: "最后修改人不能为空", trigger: "blur" }
        ],
        flastupdatetime: [
          { required: true, message: "最后修改时间不能为空", trigger: "blur" }
        ],
        fstate: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        fsourcebillid: [
          { required: true, message: "来源单ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询手工调整数据列表 */
    getList() {
      this.loading = true;
      listCwsgtzs(this.queryParams).then(response => {
        this.cwsgtzsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fid: null,
        fnumber: null,
        fcompany: null,
        fperiod: null,
        fcreator: null,
        fcreatetime: null,
        flastupdateuser: null,
        flastupdatetime: null,
        fstate: null,
        fsourcebillid: null
      };
      this.wfzCwsgtzsentryList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加手工调整数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fid = row.fid || this.ids
      getCwsgtzs(fid).then(response => {
        this.form = response.data;
        this.wfzCwsgtzsentryList = response.data.wfzCwsgtzsentryList;
        this.open = true;
        this.title = "修改手工调整数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.wfzCwsgtzsentryList = this.wfzCwsgtzsentryList;
          if (this.form.fid != null) {
            updateCwsgtzs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCwsgtzs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fids = row.fid || this.ids;
      this.$modal.confirm('是否确认删除手工调整数据编号为"' + fids + '"的数据项？').then(function() {
        return delCwsgtzs(fids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 手工调整数据分录序号 */
    rowWfzCwsgtzsentryIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 手工调整数据分录添加按钮操作 */
    handleAddWfzCwsgtzsentry() {
      let obj = {};
      obj.fcostnumber = "";
      obj.fcostname = "";
      obj.faccountnumber = "";
      obj.faccountname = "";
      obj.ftzje = "";
      obj.fzy = "";
      this.wfzCwsgtzsentryList.push(obj);
    },
    /** 手工调整数据分录删除按钮操作 */
    handleDeleteWfzCwsgtzsentry() {
      if (this.checkedWfzCwsgtzsentry.length == 0) {
        this.$modal.msgError("请先选择要删除的手工调整数据分录数据");
      } else {
        const wfzCwsgtzsentryList = this.wfzCwsgtzsentryList;
        const checkedWfzCwsgtzsentry = this.checkedWfzCwsgtzsentry;
        this.wfzCwsgtzsentryList = wfzCwsgtzsentryList.filter(function(item) {
          return checkedWfzCwsgtzsentry.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWfzCwsgtzsentrySelectionChange(selection) {
      this.checkedWfzCwsgtzsentry = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basebill/cwsgtzs/export', {
        ...this.queryParams
      }, `cwsgtzs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
